package com.wu.family.feed;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

import org.json.JSONArray;
import org.json.JSONObject;

import com.wu.family.config.Constant;
import com.wu.family.config.Urls;
import com.wu.family.model.AlarmModel;
import com.wu.family.model.AudioPhoto;
import com.wu.family.model.User;
import com.wu.family.model.UserComment;
import com.wu.family.utils.TimeUtil;
import com.wu.family.utils.NetHelper;
import com.wu.family.utils.json.JsonParse;
import com.wu.family.utils.json.MapBean;

public class FeedDataUtil {
	public enum DataType{
		COLLECTION,
		ALL,
		PERSON
	}

	public static List<Map<String, Object>> getListData2(DataType type, String uid, 
			int page, String mDate, String keyword) {
		String jsonStr = "";
		if (type == DataType.COLLECTION) {
			jsonStr = NetHelper.getDateByHttpClient(Urls.getLoveFeedList(uid, page, 
					"", "", "1", "1", "1"));
		}else {
			String view = (type==DataType.ALL)?"wme":"me";
			jsonStr = NetHelper.getDateByHttpClient(Urls.getFeedGroupByDay(uid, view, 
					page, "", mDate, "1", "1", keyword));
		}
		
		
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		Map<String, Object> item;
		
		if (jsonStr == null || jsonStr.equals("")) {
			return null;
		}
		try{
			JSONObject resultData = new JSONObject(jsonStr);
			int error = resultData.getInt("error");
			if(error == 0){
				JSONObject dataObj = resultData.getJSONObject("data");
				Iterator<String> keys = dataObj.keys();
				
				SortedSet<String> dateSet = new TreeSet<String>();
				while(keys.hasNext()){
					dateSet.add(keys.next()); 
				}
				
				List<String> dateList = new ArrayList<String>();
				for (String date : dateSet) {
					dateList.add(date); 
				}
				
				int size = dateList.size();
				for (int index = size-1; index >= 0; index--) {
					String date = dateList.get(index);
					JSONArray dateDataArr = dataObj.getJSONArray(date);

					//JSONArray dataArr = resultData.getJSONArray("data");
					int num = dateDataArr.length();
					JSONObject temp;
					 
					for(int i=0; i<num; i++){
						temp = dateDataArr.getJSONObject(i);
						item = new HashMap<String, Object>();
						
						item.put("date", date);
						item.put("uid", JsonParse.getString("uid", temp));
						item.put("username", JsonParse.getString("username", temp));
						item.put("vipstatus", JsonParse.getString("vipstatus", temp));
						item.put("dateline", temp.isNull("dateline")?(int)(new Date().getTime()/1000)
								:temp.getInt("dateline"));
						item.put("issubscribe", JsonParse.getInt("issubscribe", temp)+"");
						
						int picnum = JsonParse.getInt("picnum", temp);
						String idtype = JsonParse.getString("idtype", temp);
						
						item.put("image_1", doImageUrlSuffix(JsonParse.getString("image_1", temp), idtype, picnum));
						item.put("image_2", doImageUrlSuffix(JsonParse.getString("image_2", temp), idtype, picnum));
						item.put("image_3", doImageUrlSuffix(JsonParse.getString("image_3", temp), idtype, picnum));
						item.put("image_4", doImageUrlSuffix(JsonParse.getString("image_4", temp), idtype, picnum));
						
						item.put("image_5", doImageUrlSuffix(JsonParse.getString("image_5", temp), idtype, picnum));
						item.put("image_6", doImageUrlSuffix(JsonParse.getString("image_6", temp), idtype, picnum));
						item.put("image_7", doImageUrlSuffix(JsonParse.getString("image_7", temp), idtype, picnum));
						item.put("image_8", doImageUrlSuffix(JsonParse.getString("image_8", temp), idtype, picnum));
						item.put("image_9", doImageUrlSuffix(JsonParse.getString("image_9", temp), idtype, picnum));
						
						
						item.put("id", JsonParse.getString("id", temp));
						item.put("idtype", temp.isNull("idtype")?
								Constant.IdType.NOTICE_ID:temp.getString("idtype"));
						item.put("fid", JsonParse.getString("fid", temp));
						item.put("fuid", JsonParse.getString("fuid", temp));
						
						item.put("fusername", JsonParse.getString("fusername", temp));
						item.put("subject", JsonParse.getString("subject", temp));
						item.put("message", JsonParse.getString("message", temp));
						item.put("title", JsonParse.getString("title", temp));
						item.put("replynum", JsonParse.getString("replynum", temp));
						
						item.put("reblognum", JsonParse.getString("reblognum", temp));
						item.put("love", JsonParse.getString("love", temp));
						item.put("lat", JsonParse.getLong("lat", temp)+"");
						item.put("lng", JsonParse.getLong("lng", temp)+"");
						item.put("name", JsonParse.getString("name", temp));
						
						item.put("location", JsonParse.getString("location", temp));
						item.put("come", JsonParse.getString("come", temp));
						item.put("picnum", JsonParse.getInt("picnum", temp));
						item.put("note", JsonParse.getString("note", temp));
						item.put("fname", JsonParse.getString("fname", temp));
						item.put("mylove", JsonParse.getInt("mylove", temp) + "");
						
						item.put("cuid", JsonParse.getString("cuid", temp));
						item.put("cname", JsonParse.getString("cname", temp));
						item.put("cavatar", JsonParse.getString("cavatar", temp));
						item.put("cvipstatus", JsonParse.getString("cvipstatus", temp));
						
						item.put("fsubject", JsonParse.getString("fsubject", temp));
						item.put("fmessage", JsonParse.getString("fmessage", temp));
						item.put("avatar", null);
						item.put("avatarPath", JsonParse.getString("avatar", temp).replace("small", "middle"));
						
						/*item.put("soundfile", JsonParse.getString("soundfile", temp));
						item.put("soundtime", JsonParse.getLong("soundtime", temp));
						item.put("soundurl", JsonParse.getString("soundurl", temp));//
						item.put("isplaying", false);*/
						
						AudioPhoto audioPhoto = new AudioPhoto();
						audioPhoto.setAudioUrl(JsonParse.getString("soundurl", temp));
						audioPhoto.setAudioTime(JsonParse.getLong("soundtime", temp));
						audioPhoto.setPlaying(false);
						item.put("AudioPhoto", audioPhoto);
						
						if (!temp.isNull("tag") && !temp.getString("tag").equals("[]")) {
							JSONObject tag = temp.getJSONObject("tag");
							item.put("tagid", tag.getInt("tagid")+"");
							item.put("tagname", tag.getString("tagname"));
						}
						item.put("commentnum", 0);
						if (!temp.isNull("comment")) {
							JSONArray comments = temp.getJSONArray("comment");
							int length = comments.length();
							item.put("commentnum", length);
							
							ArrayList<UserComment> userComments = new ArrayList<UserComment>(length+5);
							UserComment userComment;
							for(int j=0; j < length/* && j < 2*/; j++){
								JSONObject comment = comments.getJSONObject(j);
								userComment = new UserComment();
								userComment.getUser().setUid(comment.getString("authorid"));
								userComment.getUser().setName(comment.getString("authorname"));
								userComment.setCid(JsonParse.getString("cid", comment));
								userComment.setMessage(comment.getString("message"));
								userComment.setDateline(comment.getLong("dateline"));
								userComment.getAudio().setAudioId(JsonParse.getString("soundid", comment));
								userComment.getAudio().setAudioUrl(JsonParse.getString("soundurl", comment));
								userComment.getAudio().setAudioTime((float)JsonParse.getDouble("soundtime", comment));
								
								
								item.put("authorid"+j, comment.getString("authorid"));
								item.put("dateline"+j, TimeUtil.transTime(comment.getInt("dateline")));
								item.put("message"+j, comment.getString("message"));
								item.put("authorname"+j, comment.getString("authorname"));
								
								userComments.add(userComment);
							}
							item.put("UserCommentList", userComments);
						}
						
						if (!temp.isNull("loveuser")) {
							List<MapBean> loveuserMap = JsonParse.parseToListMap(
									JsonParse.getString("loveuser", temp));
							item.put("loveuser", loveuserMap);
						}
						
						item.put("calendarnum", !temp.isNull("calendarnum")
								?temp.getInt("calendarnum")+"":"0");
						if (!temp.isNull("calendarinfo")) {
							MapBean calendarinfo = JsonParse.parseToObjMap(
									temp.getJSONObject("calendarinfo"));
							item.put("calendarinfo", calendarinfo);
							
							AlarmModel model = new AlarmModel();
							JSONObject calendarJObj = temp.getJSONObject("calendarinfo");
							model.setAlarmid(calendarJObj.getString("id"));
							model.setSubject(calendarJObj.getString("subject").replaceAll("&nbsp;", " "));
							model.setTime(calendarJObj.getString("time"));
							model.setYear(calendarJObj.getString("year"));
							model.setMonth(calendarJObj.getString("month"));
							model.setDay(calendarJObj.getString("day"));
							model.setWday(calendarJObj.getString("wday"));
							model.setUid(calendarJObj.getString("uid"));
							model.setRepeatday(calendarJObj.getString("repeatday"));
							model.setIdtype(calendarJObj.getString("idtype"));
							
							JSONArray calendarto = JsonParse.getJSONArray("calendarto", calendarJObj);
							if (calendarto != null && calendarto.length() > 0) {
								ArrayList<User> users = new ArrayList<User>();
								for (int j = 0; j < calendarto.length(); j++) {
									JSONObject JObj = calendarto.getJSONObject(j);
									User user = new User();
									user.setUid(JsonParse.getString("uid", JObj));
									user.setUsername(JsonParse.getString("username", JObj));
									user.setName(JsonParse.getString("name", JObj));
									user.setAvatar(JsonParse.getString("avatar", JObj));
									user.setVipstatus(JsonParse.getString("vipstatus", JObj));
									users.add(user);
								}
								model.setUserList(users);
							}
							
							item.put("AlarmModel", model);
						}
						
						data.add(item);
					}
				}
			}else {
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		//page++;
		return data;
	}
	
	public static String doImageUrlSuffix(String url, String idtype, int picnum) {
		if (url.startsWith("http://") && url.contains("upaiyun")) {
			if (!url.contains("!600")) {
				url = url + "!600";
			}
			if (picnum > 1) {
				url = url.replace("!600", "!190X190");
			}else {
				if (idtype.contains("photoid")) {
					url = url.replace("!600", "!210"); //"!580X400"
				}else if (idtype.contains("comment")) {
					url = url.replace("!600", "!90X90");
				}else {
					url = url.replace("!600", "!190X190");//"!200X280"
				}
			}
			
			return url;
		}else {
			return url;
		}
	}
	
	
}
